package com.beiding.my.bemapping;


import java.lang.reflect.Field;

public class BeQuoteMapper implements BeAttributeMapper {

    Field quoteFiled;

    BeTypeMapper realBoTypeMapper;

    private String id = Id.boq();

    String name;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void assignmentByAttributeName(Object be, String[] segments, int currentSegment, Object v) {
        try {
            Object t = quoteFiled.get(be);
            if (t == null) {
                t = realBoTypeMapper.newBe();
                //执行赋值操作
                quoteFiled.set(be, t);
            }
            realBoTypeMapper.assignmentByAttributeName(t, segments, currentSegment + 1, v);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void assignmentByAttributeId(Object be, String[] segments, int currentSegment, Object v) {
        try {
            Object t = quoteFiled.get(be);
            if (t == null) {
                t = realBoTypeMapper.newBe();
                //执行赋值操作
                quoteFiled.set(be, t);
            }
            realBoTypeMapper.assignmentByAttributeId(t, segments, currentSegment + 1, v);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void fillExpressionDetails(Class type, String[] segments, int currentSegment, ExpressionDetails expressionDetails) {
        expressionDetails.quoteIds.add(this.id);
        QuoteDetails quoteDetails = StaticQuoteHolder.createQuoteDetails(type, this.realBoTypeMapper.dtTypeMapper.getType(), this.name);
        quoteDetails.holderStatement = expressionDetails.lastQuoteDetails.statement;
        quoteDetails.statement = expressionDetails.lastQuoteDetails.statement + "_" + segments[currentSegment];
        expressionDetails.quoteDetails.add(quoteDetails);
        expressionDetails.lastQuoteDetails = quoteDetails;
        realBoTypeMapper.fillExpressionDetails(segments, currentSegment + 1, expressionDetails);
    }

}
